﻿using MyHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace weighing
{
    /// <summary>
    /// OrtherPrintUc.xaml 的交互逻辑
    /// </summary>
    public partial class OrtherPrintUc : System.Windows.Controls.UserControl
    {
        private WeighingBillVo mWeighingBillVo;

        public OrtherPrintUc()
        {
            InitializeComponent();
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) return;
            this.Loaded += OutPrint_Loaded;
            this.DataContextChanged += OutPrint_DataContextChanged;

            HideCalculatePanel();

            ShowTime();
        }

        private void OutPrint_Loaded(object sender, RoutedEventArgs e)
        {
            SetBillDesc();
        }

        private void OutPrint_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext is WeighingBillVo bill)
            {
                mWeighingBillVo = bill;
               // GeneraterQrCode();
               switch((WeightingBillType)mWeighingBillVo.Type)
                { 
                    case WeightingBillType.Self:
                        this.subTitleTb.Text = "(内部倒运磅单)";
                    break;

                    case WeightingBillType.Foreign:
                        this.subTitleTb.Text = "(外来过磅磅单)";
                        break;
                }
            }
        }


        private void SetBillDesc()
        {
            this.billHeaderTb.Text = App.CurrentCompanyVo.FullName;

            var def = " 白联存根，红联财务，黄联运费，绿联随车，其它";
            var desc = App.Config.BillPrintConfig.OutDesc;
            this.descTb.Text = desc.IsNullOrEmpty() ? def : desc;

        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        private void GeneraterQrCode()
        {
            //是否开启对像存储 
            switch (App.Config.ImgConfig.OosType)
            {
                case OosType.Local:
                    ShowBillNumberQrCode(mWeighingBillVo.Number);
                    break;
                case OosType.AliYun:
                    ShowAliyunOosQrCode();
                    break;
                case OosType.QiniuYun:
                    ShowQiniuOosQrCode();
                    break;
                default:
                    ShowBillNumberQrCode(mWeighingBillVo.Number);
                    break;
            }
        }

        /// <summary>
        /// 以磅单编号，生成和显示 二维码
        /// </summary>
        private void ShowBillNumberQrCode(String number)
        {
            //if (number.IsNullOrEmpty() || number.Length < 8)
            //{
            //    ServiceFactory.SysMessageService.Info("以磅单编号生成二维码时，磅单编号长度应超过8位");
            //    return;
            //}
            //var bitmap = MyHelper.QrCode.QrCodeHelper.GenerateQrCode(number, 90, 90);
            //this.codeImage.Source = BitmapHelper.ToBitmapImage(bitmap);
        }

        /// <summary>
        /// 以阿里云OOS 地址生成二维码
        /// </summary>
        private void ShowAliyunOosQrCode()
        {
            try
            {
                //string fileName = mWeighingBillVo.Number + Constract.BillCaputureSuffix;
                //string key = AliOOSHelper.Insance.GetKey(fileName);
                //string url = AliOOSHelper.Insance.BuildRemoteUrl(key);
                //var bitmap = MyHelper.QrCode.QrCodeHelper.GenerateQrCode(url, 90, 90);
                //this.codeImage.Source = BitmapHelper.ToBitmapImage(bitmap);
            }
            catch (Exception e)
            {
                Log.Error("生成 阿里云OOS 地址生成二维码出错：" + e.Message,e.StackTrace,true);
            }

        }
        /// <summary>
        ///TODO 以七牛云OOS 地址生成二维码
        /// </summary>
        private void ShowQiniuOosQrCode()
        { //开发中
            try
            {

            }
            catch (Exception e)
            {
               Log.Error("生成 七牛云OOS 地址生成二维码出错：" + e.Message,e.StackTrace,true);
            }
        }

        /// <summary>
        /// 隐藏结算区域信息
        /// </summary>
        private void HideCalculatePanel()
        {
            this.PriceTbLabel.Visibility = Visibility.Collapsed;
            this.PriceTb.Visibility = Visibility.Collapsed;
            this.TotalMoneyTbLabel.Visibility = Visibility.Collapsed;
            this.TotalMoneyTb.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// 显示过磅时间
        /// </summary>
        private void ShowTime()
        {
            if (App.Config.WeighingConfig.OutTimes == 2)
            {
                this.InTimePanel.Visibility = Visibility.Visible;
                this.OutTimePanel.Visibility = Visibility.Visible;
                this.NetWeighingTimePanel.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.InTimePanel.Visibility = Visibility.Collapsed;
                this.OutTimePanel.Visibility = Visibility.Collapsed;
                this.NetWeighingTimePanel.Visibility = Visibility.Visible;
            }
        }

    }
}